package com.zzyl.nursing.controller;

import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.nursing.vo.DeviceDetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * IoT设备管理Controller
 * 
 * @author zjx
 * @date 2025-05-25
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "IoT设备管理相关接口")
public class DeviceController extends BaseController
{
    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询IoT设备列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询设备列表")
    public TableDataInfo<List<Device>> list(Device device)
    {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 从物联网平台同步产品列表
     * @return
     */
    @ApiOperation("从物联网平台同步产品列表")
    @PostMapping("/syncProductList")
    public AjaxResult getDeviceList(){
        deviceService.getDeviceList();
        return AjaxResult.success();
    }

    /**
     * 查询所有产品列表
     * @return
     */
    @GetMapping("/allProduct")
    @ApiOperation(value = "查询所有产品列表")
    public AjaxResult allProduct() {
        return success(deviceService.allProduct());
    }

    /**
     * 注册设备到IoT平台
     * @param deviceDto
     * @return
     */
    @PostMapping("/register")
    @ApiOperation(value = "注册设备")
    public AjaxResult registerDevice(@RequestBody DeviceDto deviceDto) {
        deviceService.register(deviceDto);
        return success();
    }

    /**
     * 查询设备详情
     * @return
     */
    @PostMapping("/queryDeviceDetail")
    @ApiOperation(value = "查询设备详情")
    public AjaxResult queryDeviceDetail(@RequestBody DeviceDto dto) {
       DeviceDetailVo vo  = deviceService.queryDeviceDetail(dto.getIotId());

        return success(vo);
    }

    /**
     * 查看指定产品的已发布物模型中的功能定义详情
     */
    @PostMapping("/queryProductModelInfo")
    @ApiOperation("查看指定产品的已发布物模型中的功能定义详情")
    public AjaxResult queryProductModelInfo(@RequestBody DeviceDto dto) {
        return success(deviceService.queryProductModelInfo(dto.getProductKey()));
    }

    /**
     * 查询指定设备的物模型运行状态
     */
    @PostMapping("/queryDevicePropertyStatus")
    @ApiOperation("查询指定设备的物模型运行状态")
    public AjaxResult queryDevicePropertyStatus(@RequestBody DeviceDto dto) {
       Map<String,Object> map  = deviceService.queryDevicePropertyStatus(dto.getDeviceName());
        return success(map);
    }

    @PutMapping
    @ApiOperation("修改设备备注名称")
    public AjaxResult updateNickName(@RequestBody Device device){
        deviceService.updateNickName(device);
        return AjaxResult.success();
    }

    @DeleteMapping
    @ApiOperation("删除设备")
    public void delete(@RequestBody Device device){
        deviceService.delete(device.getIotId());
    }
}
